#include <TVout.h>
#include <fontALL.h>
#include <pollserial.h>
#define W 128
#define H 96
#define BITWIDTH 5
#define THRESHOLD 3

TVout tv;
pollserial pserial;
unsigned char x,y;
char s[32];
//int start = 40;
unsigned char ccdata[16];
// VBI_Inserter with _delay_loop_1(44);
//byte bpos[][8] = {{ 8,  14,  21, 27, 24, 40, 47, 54 },   {61, 67, 74, 80, 87,  93,  99, 105}};
byte bpos[][8] = {{ 0,   8,  15, 21, 27, 34, 40, 47 },   {54, 61, 67, 74, 80,  87,  92,  99}};
boolean wroteOutput = false;
//boolean newline = false;
char c[2];
//char lastControlCode[2];
int line = 43;
int dataCaptureStart = 225;

void setup()  {
  tv.begin(_PAL, W, H);
  tv.set_hbi_hook(pserial.begin(115200));
  initOverlay();
  initInputProcessing();

  tv.setDataCapture(line, dataCaptureStart, ccdata);

  y = 0;

  tv.select_font(font6x8);
  tv.fill(0);

  // uncomment this to display the bit positions on the screen
  // for alignment/debugging
}

void loop() {
  byte pxsum;
  byte i;

  // Display the captured data line to the screen so we can see it.
  
  
  if (!wroteOutput) {
    //displayBitPositions();
    displayccdata();
    OutputData();
    for(byte bytenum=0;bytenum<2;bytenum++) {
      c[bytenum] = 0;
      for(int bit=0;bit<8;bit++) {
		pxsum = 0;
		for(int w=0;w<BITWIDTH;w++) {
		  i = bpos[bytenum][bit]+w;
		  if (((ccdata[i/8] >> (7 - (i%8))) & 1) == 1) {
			pxsum++;
		  }
		}
		if (pxsum >= THRESHOLD) {
		  c[bytenum] |= (1 << (7-bit));// bit is "on"
		}
      }
    }    

    tv.print(c[0]);
    tv.print(c[1]);
    //pserial.write(c[0]);
    //pserial.write(c[1]);
    
    for(x=0;x<display.hres;x++) {
      ccdata[x] = 0;
    }
    // output the data
	
  }
}

void initOverlay() {
  TCCR1A = 0;
  // Enable timer1.  ICES0 is set to 0 for falling edge detection on input capture pin.
  TCCR1B = _BV(CS10);

  // Enable input capture interrupt
  TIMSK1 |= _BV(ICIE1);

  // Enable external interrupt INT0 on pin 2 with falling edge.
  EIMSK = _BV(INT0);
  EICRA = _BV(ISC11);
}

void initInputProcessing() {
  // Analog Comparator setup
  ADCSRA &= ~_BV(ADEN); // disable ADC
  ADCSRB |= _BV(ACME); // enable ADC multiplexer
  ADMUX &= ~_BV(MUX0);  // select A2 for use as AIN1 (negative voltage of comparator)
  ADMUX |= _BV(MUX1);
  ADMUX &= ~_BV(MUX2);
  ACSR &= ~_BV(ACIE);  // disable analog comparator interrupts
  ACSR &= ~_BV(ACIC);  // disable analog comparator input capture
}



ISR(INT0_vect) {	//New frame
  display.scanLine = 0;
  wroteOutput = false;
}



void displayccdata() {// display the captured data line on the screen
  y = 0;
  pserial.write("!:!");
  for(x=0;x<display.hres;x++) {
    //display.screen[(y*display.hres)+x] = ccdata[x];
    pserial.write(ccdata[x]);
  }
}



int getValue() {
  int value;
  ADCSRA |= _BV(ADEN); // enable ADC
  value = analogRead(5);
  initInputProcessing();
  return value;
}



void OutputData() {

        c[0] = 0;
		c[1] = 0;

        wroteOutput = true;
}


void displayBitPositions() {

  y = 1;
  tv.draw_line(0, y, W-1, y, 0);
  for(byte bytenum=0;bytenum<2;bytenum++) {
    for(byte bit=0;bit<8;bit++) {
      tv.set_pixel(bpos[bytenum][bit], y, 1);
    }
  }
  
}
